<?php
/**
 * Created by PhpStorm.
 * User: chy
 * Date: 2017/9/26
 * Time: 下午8:10
 */

class m_user extends CI_Model
{


    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    function getdata(){
        $query=$this->db->get('user');
        return $query->result();
    }
    function insert($arr){

        $this->db->insert('user',$arr);
        return true;
    }
    function in_recycle($user_id)
    {
        $this->db->set('user_status',0);
        $this->db->where('user_id',$user_id);
        //$this->db->delete('lib_bookinfo');这里做的不是将内容删除而是将删除标志置为1

        $this->db->update('user');
        return true;
    }
    /*
    function out_recycle($bac_id)
    {
        $this->db->set('bac_del', 1);
        $this->db->where('bac_id', $bac_id);
        return $this->db->update('bacteria');
    }
    */
    function update($user_id,$arr){

        $this->db->where('user_id',$user_id);

        $this->db->update('user',$arr);
        return true;
    }
    function queryById($user_id){
        if(!$user_id)
        {
            return array();
        }
        //的到书籍信息
        $sql="select user_id,user_name,user_info,role_id,user_status,lastLoginTime,user_part from user where user_id='$user_id'";
        //$query = $this->db->get_where('bacteria',array('bac_id'=>$bac_id));
        //$row = $query->result_array();
        $query = $this->db->query($sql);
        $temp = $query->result_array();
        //print_r($temp);
        //echo $bac_id;
        return $temp[0];
    }
    function find_user($options = array(),$count=20, $offset)
    {
        if(!is_array($options))
        {
            //echo '没有设置查询规则books_model line 229';
            return array();
        }
        if ($count)
        {
            //$this->db->limit((int)$count, (int)$offset);
            $limit = "limit $offset , $count";
        }

        $sql = "select user_id,user_name,user_info,role_id,user_status,lastLoginTime,user_part from user ".$limit;
        //echo $sql;
        $query = $this->db->query($sql);
        //$rows = $query->row_array();
        // print_r($rows);
        $temp = $query->result_array();
        //print_r($temp);


        return $temp;
    }
    function find_user_count($options = array())
    {
        if(!is_array($options))
        {
            //echo '没有设置查询规则books_model line 229';
            return array();
        }

        //没有图书类型，只有查询方式和相应关键字

        //查询方式不为空且关键字不为空

        $sql = "select user_id,user_name,user_info,role_id,user_status,lastLoginTime,user_part from user ";

        $query = $this->db->query($sql);
        return $query;
    }
    function getDataCount($options = array(),$user_status=1){
        $query = $this->find_user_count($options,$user_status);
        $total = $query->num_rows();
        return $total;
    }
    function give_role($user_id,$role_id){
        $this->db->set('role_id',$role_id);
        $this->db->where('user_id',$user_id);
        $this->db->update('user');
        return true;
    }
    function update_password($user_id,$user_password,$new_password){
        $sql="select * from user where user_id='$user_id' and user_status='1'";
        $query = $this->db->query($sql);
        $temp = $query->result_array();
        if(password_verify($user_password, $temp[0]['user_password'])){
            $user_password_hash = password_hash($new_password, PASSWORD_DEFAULT, ['cost' => 12]);
            $this->db->set('user_password',$user_password_hash);
            $this->db->where('user_id',$user_id);
            $this->db->update('user');
            return true;
        }else
            return false;
    }
}
